<script setup>
    import { ref ,reactive } from 'vue'
    import { Plus } from '@element-plus/icons-vue'

    let value1 = ref("");
    let value2 = ref("");
    let searchVal = ref("");
    let tableData=ref([{}]);
    let pageSize=ref(10);
    let currentPage=ref(1);
    let dialogVisible = ref(false)        //对话框是否显示
    // let imageUrl = ref("");
    let form = ref({
        imageUrl:'',
        status:false
    })
    const formRef = ref({})             // ref
    const rules = reactive({
        name: [
            {
            required: true,
            message: '请输入轮播图名称',
            trigger: 'blur',
            },
        ],
        imageUrl: [
            {
            required: true,
            message: '请选择图片',
            },
        ],
        url: [
            {
            required: true,
            message: '请输入轮播图链接地址',
            trigger: 'blur',
            },
        ],
        sort: [
            {
            required: true,
            message: '请排序(数字)',
            trigger: 'blur',
            },
        ],
    })

    const handleEdit =  ()=>{           // 编辑
        dialogVisible.value = !dialogVisible.value
    };
    const handleDelete =  ()=>{         // 删除

    };
    const handleSizeChange =  ()=>{     // 每页条数改变

    };
    const handleCurrentChange =  ()=>{      // 当前页改变

    };
    const changeDialogVisible = ()=>{       // 改变对话框是否显示
        dialogVisible.value = !dialogVisible.value
    };
    const handleAvatarSuccess = ()=>{       // 图片上传成功的回调函数

    };
    const beforeAvatarUpload = (file)=>{        // 图片上传之前的回调函数
        console.log(file);
        form.imageUrl = file.name
    };
    const onSubmit = (formRules)=>{           // 点击提交
        formRules.validate(async (valid)=>{            //表单校验的回调函数
            if(valid){                      //校验成功
                console.log(form);
            }else{                          //校验失败
                console.log("校验失败");
                return false
            }
        })
    };
    const handleClose = ()=>{           // 弹框关闭前的回调函数
        form.value = {
        imageUrl:'',
        status:false
        }
        console.log(form);
        dialogVisible.value = !dialogVisible.value
    };
</script>

<template>
<div style="padding: 10px">
    <!-- 搜索框、新增按钮部分 -->
    <div class="top">
        <el-row>
            <el-col :span="5">
                <el-input v-model="searchVal" placeholder="Please input" />
            </el-col>
            <el-col :span="15" :offset="1">
                <span>上传时间：</span>
                <el-date-picker v-model="value1" type="date" placeholder="Pick a day">
                </el-date-picker>
                <span style="padding: 0 10px">-</span>
                <el-date-picker v-model="value2" type="date" placeholder="Pick a day">
                </el-date-picker>
                <el-button type="primary" style="margin-left: 18px">筛选</el-button>
            </el-col>
            <el-col :span="3">
                <div class="addBtn">
                    <el-button type="success" @click="changeDialogVisible">新增轮播图</el-button>
                </div>
            </el-col>
        </el-row>
    </div>
    <!-- 表格部分 -->
    <div class="tableBox">
        <el-table :data="tableData" style="width: 100%" :border="true" max-height="250">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column prop="name" label="轮播图标题" width="200" align="center"/>
            <el-table-column prop="state" label="缩略图" width="240" align="center"/>
            <el-table-column prop="city" label="状态" width="150" align="center"/>
            <el-table-column prop="address" label="上传人" width="150" align="center"/>
            <el-table-column prop="zip" label="上传时间" width="auto" align="center"/>
            <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
                <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                size="small"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                >
                    删除
                </el-button>
            </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="fenyeBox">
            <el-pagination
            v-model:currentPage="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[100, 200, 300, 400]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="400"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            >
            </el-pagination>
        </div>
        
    </div>
    <!-- 弹框 -->
    <div class="dialogBox">
        <el-dialog
            v-model="dialogVisible"
            title="新增轮播图"
            width="40%"
            :before-close="handleClose"
        >
            <el-form ref="formRef" :model="form" :rules="rules" label-width="130px">
                <el-form-item label="轮播图标题" prop="name">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="轮播图" prop="imageUrl">
                    <el-upload
                        class="avatar-uploader"
                        action="https://jsonplaceholder.typicode.com/posts/"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload"
                    >
                        <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon"><plus /></el-icon>
                    </el-upload>
                </el-form-item>
                <el-form-item label="轮播图状态">
                    <el-switch
                        v-model="form.status"
                        class="ml-2"
                        inline-prompt
                        active-color="#13ce66"
                        active-text="Y"
                        inactive-text="N"
                    />
                </el-form-item>
                <el-form-item label="轮播图链接地址" prop="url">
                    <el-input v-model="form.url"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="form.sort"></el-input>
                    <span>填写数字，数字越大越靠前</span>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit(formRef)">提交</el-button>
                    <el-button @click="dialogVisible = false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
    
</div>
</template>

<style scoped>
.top:deep() .el-date-editor{
    width:28% !important;
}
.addBtn{
    padding-right: 10px;
    display: flex;
    justify-content: end;
}
.tableBox{
    text-align: center;
    margin-top:10px;
}
.dialogBox:deep() .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.dialogBox:deep() .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.dialogBox:deep() .el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.dialogBox:deep() .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.fenyeBox{
    margin-top:10px;
}
</style>